﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApiTest.Dto.Basic;
using WebApiTest.Dto.Utils;
using WebApiTest.Models;

namespace WebApiTest.Service.Basic

{
    public class UserService
    {
        officeContext db = new officeContext();

        public TableRspDTO list4Table(TableReqDTO tableReqDTO)
        {
            long count = db.BasicUser
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("loginName"))|| u.LoginName.Contains(tableReqDTO.parseQueryParam("loginName")))
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("userName")) || u.UserName.Contains(tableReqDTO.parseQueryParam("userName")))
                .LongCount();
            List<JObject> res = db.BasicUser
                .Select(u => new { key = u.UserId, userId = u.UserId, loginName = u.LoginName, roleId = u.RoleId, userName = u.UserName, userInfo = u.UserInfo, gmtCreatr = u.GmtCreate })
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("loginName")) || u.loginName.Contains(tableReqDTO.parseQueryParam("loginName")))
                .Where(u => string.IsNullOrEmpty(tableReqDTO.parseQueryParam("userName")) || u.userName.Contains(tableReqDTO.parseQueryParam("userName")))
                .OrderByDescending(u => u.userId)
                .Skip(tableReqDTO.getStart())
                .Take(tableReqDTO.pageSize)
                .ToList()
                .ConvertAll(r => JObject.FromObject(r));
            PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.pageNo,
                tableReqDTO.pageSize,
                count,
                res
                );
            return new TableRspDTO(pagingDTO);
        }

        public int add(BasicUser basicUser)
        {
            basicUser.GmtCreate = DateTime.Now;
            basicUser.LoginPwd = JWTUtil.MD5Encrypt("hbh@1234");
            db.BasicUser.Add(basicUser);
            db.SaveChanges();
            return 1;
        }
        public int update(BasicUser basicUser)
        {
            BasicUser user = db.BasicUser.Find(basicUser.UserId);
            user.LoginName = basicUser.LoginName;
            user.UserName = basicUser.UserName;
            user.RoleId = basicUser.RoleId;
            user.UserInfo = basicUser.UserInfo;
            db.SaveChanges();
            return 1;
        }
        public int remove(string ids)
        {
            List<BasicUser> list = new List<BasicUser>();
            string[] idList = ids.Split(",");
            foreach (string id in idList)
            {
                list.Add(db.BasicUser.Find(long.Parse(id)));
            }
            db.BasicUser.RemoveRange(list);
            //更新到数据库
            db.SaveChanges();
            return list.Count();
        }

        public int resetPwd(JObject request)
        {
            long userId = long.Parse(request["userId"].ToString());
            string loginPwd = request["loginPwd"].ToString();
            BasicUser user = db.BasicUser.Find(userId);
            user.LoginPwd = JWTUtil.MD5Encrypt(loginPwd);
            db.SaveChanges();
            return 1;
        }

        public BasicUser loginCheck(String loginName)
        {
            List<BasicUser> res = db.BasicUser.Where(user => user.LoginName.Equals(loginName)).ToList();
            if(res.Count > 0)
            {
                return res[0];
            }
            else
            {
                return null;
            }
        }

        public BasicUser getByUserId(long userId)
        {
            List<BasicUser> res = db.BasicUser.Where(user => user.UserId.Equals(userId)).ToList();
            if (res.Count > 0)
            {
                return res[0];
            }
            else
            {
                return null;
            }
        }


    }
}
